﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
namespace HF.WorkFlow.Template
{
    /// <summary>
    /// 数据访问类SubWorkFlow。
    /// </summary>
    public class SubWorkFlow
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "SubWFId", "WorkFlowId", "WorkTaskId", "SubWorkFlowId", "SubWorkFlowCaption", "SubStartTaskId", "Description" };
        private string tableName = "HF_SubWorkFlow";
        private string keyField = "WorkTaskId";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(SubWorkFlowEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@SubWFId", entity.SubWFId, typeof(string));
            sqlDataItem.AppendParameter("@WorkFlowId", entity.WorkFlowId, typeof(string));
            sqlDataItem.AppendParameter("@WorkTaskId", entity.WorkTaskId, typeof(string));
            sqlDataItem.AppendParameter("@SubWorkFlowId", entity.SubWorkFlowId, typeof(string));
            sqlDataItem.AppendParameter("@SubWorkFlowCaption", entity.SubWorkFlowCaption, typeof(string));
            sqlDataItem.AppendParameter("@SubStartTaskId", entity.SubStartTaskId, typeof(string));
            sqlDataItem.AppendParameter("@Description", entity.Description, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

        public SubWorkFlowEntity GetSubWorkFlowEntity(DataRow dr)
        {
            SubWorkFlowEntity entity = new SubWorkFlowEntity();
            string value = "";
            entity.SubWFId = dr["SubWFId"].ToString();
            entity.WorkFlowId = dr["WorkFlowId"].ToString();
            entity.WorkTaskId = dr["WorkTaskId"].ToString();
            entity.SubWorkFlowId = dr["SubWorkFlowId"].ToString();
            entity.SubWorkFlowCaption = dr["SubWorkFlowCaption"].ToString();
            entity.SubStartTaskId = dr["SubStartTaskId"].ToString();
            entity.Description = dr["Description"].ToString();
            return entity;
        }

        #endregion

        #region  构造函数
        public SubWorkFlow()
        { }
        public static SubWorkFlow GetInstance()
        {
            return new SubWorkFlow();
        }
        #endregion

        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(SubWorkFlowEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(SubWorkFlowEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string workTaskId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, workTaskId);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }
        /// <summary>
        /// 子流程的配置记录是否存在
        /// </summary>
        public bool ExistsBySubId(string subId)
        {
            string tmpSql = "select * from HF_SubWorkFlow where SubWFId=@SubWFId";
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@SubWFId", subId);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="subWFId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string workTaskId)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="SubWFId">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetSubWorkFlowTable(string workTaskId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="subWFId">主键Id</param>
        /// <returns>DataTable</returns>
        public SubWorkFlowEntity GetSubWorkFlowEntity(string workTaskId)
        {
            DataTable dt = GetSubWorkFlowTable(workTaskId);
            if (dt != null && dt.Rows.Count > 0)
            {
                return GetSubWorkFlowEntity(dt.Rows[0]);
            }
            return null;
        }

        #endregion

        #region  扩展方法
        //此处添加扩展方法

        public  int Delete(string workFlowId, string workTaskId)
        {

            try
            {
                string sqlStr = "delete from HF_SubWorkFlow where WorkFlowId=@WorkFlowId and WorkTaskId=@WorkTaskId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@WorkFlowId", workFlowId);
                sqlItem.AppendParameter("@WorkTaskId", workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得子流程配置信息
        /// </summary>
        /// <param name="workFlowId">流程模板Id</param>
        /// <param name="workTaskId">任务模板Id</param>
        /// <returns>DataTable</returns>
        public  DataTable GetSubWorkFlowTable(string workFlowId, string workTaskId)
        {
            try
            {
                string sqlStr = "select * from HF_SubWorkFlow where WorkFlowId=@WorkFlowId and WorkTaskId=@WorkTaskId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@WorkFlowId", workFlowId);
                sqlItem.AppendParameter("@WorkTaskId", workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 判断子流程配置信息是否存在
        /// </summary>
        /// <param name="workFlowId">流程模板Id</param>
        /// <param name="workTaskId">任务模板Id</param>
        /// <returns></returns>
        public  bool Exists(string workFlowId, string workTaskId)
        {
            string sqlStr = "select * from HF_SubWorkFlow where  WorkFlowId=@WorkFlowId and WorkTaskId=@WorkTaskId";
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = sqlStr;
            sqlItem.AppendParameter("@WorkFlowId", workFlowId);
            sqlItem.AppendParameter("@WorkTaskId", workTaskId);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);

        }
        /// <summary>
        /// 获得流程的所有子流程
        /// </summary>
        /// <param name="workflowId">流程Id</param>
        /// <returns></returns>
        public  DataTable GetSubWorkFlowOfFlow(string workflowId)
        {
            try
            {
                string sqlStr = "select * from HF_SubWorkFlow where WorkFlowId=@WorkFlowId ";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@WorkFlowId", workflowId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #endregion

    }
}

